package com.lgt_enterprise_android.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;

import android.view.Gravity;
import android.widget.Toast;

import com.lgt_enterprise_android.base.BaseActivity;


public class DialogUtils {

    public static String TAG = "DialogUtils";

    /**
     * Show the given string with toast
     *
     * @param context
     * @param msg
     */

    public static void showToast(Context context, String msg) {
        Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }


    /**
     * Show dialog with ok and cancel button
     *
     * @param context
     * @param title          Title of dialog
     * @param message        message
     * @param dialogListener Click listener
     */

    public static void showOkCancelDialog(Context context, String title, String message, final DialogListener dialogListener) {

        final AlertDialog.Builder builder =
                new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                dialogListener.onOkClick();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                dialogListener.onCancelClick();
            }
        });
        builder.show();
    }

    /**
     * Show dialog with OK positive button
     *
     * @param context
     * @param title
     * @param message
     */
    public static void showOkDialog(Context context, String title, String message) {

        final AlertDialog.Builder builder =
                new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("确定", null);

        builder.show();
    }

    /**
     * Show dialog with OK positive button
     *
     * @param context
     * @param title
     * @param message
     * @param dialogListener
     * @return
     */
    public static AlertDialog showOkDialog(Activity context, String title, String message, String setPositiveButtonLabel, final DialogInterface.OnClickListener dialogListener) {
        if (context == null || context.isFinishing())
            return null;

        if (StringUtil.isEmpty(setPositiveButtonLabel)) {
            setPositiveButtonLabel = "确定";
        }

        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);

        builder.setPositiveButton(setPositiveButtonLabel, dialogListener);
        builder.setCancelable(false);
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                dialogListener.onClick(dialogInterface, 0);
            }
        });
        AlertDialog d = builder.create();
        builder.show();
        return d;

    }

    public static ProgressDialog getProgressDialog(Context context, String message) {
        if (context == null)
            return null;

        if (((BaseActivity) context).isFinishing()) {
            return null;
        }

        ProgressDialog pd = new ProgressDialog(context);
        if (!StringUtil.isEmpty(message)){
            pd.setMessage(message);
        }else {
            pd.setMessage("加载中...");
        }

        pd.setCancelable(true);
        pd.show();
        return pd;
    }


    public interface DialogListener {
        void onOkClick();

        void onCancelClick();
    }

    /**
     * Show Dialog with positive and negative button
     *
     * @param context
     * @param message        message
     * @param ok             Label of positive button
     * @param cancel         Label of negative button
     * @param dialogListener click listener
     */
    public static void showOkCancelDialog(Context context, String message, String ok, String cancel, final DialogListener dialogListener) {
        final AlertDialog.Builder builder =new AlertDialog.Builder(context);
        builder.setMessage(message);
        builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                dialogListener.onOkClick();
            }
        });
        builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                dialogListener.onCancelClick();
            }
        });
        builder.show();
    }


}